IndexRecommendManager=function(){
	this.inited=false;
	this.options={};
	this.appVM=null;
	this.shiwenIdWithMoreList=[];
}

IndexRecommendManager.defValue={
	appVMConfig:{
		el:"#index-recommend-content",
		data:{
			recommendList:[],
		}
	},
	defGushiwenWriterVo:{
		id:0,
		headImageUrl:"./images/defWriterHeader001.jpg",
		simpleIntro:" 暂未收录作者信息"
	},
	loadOver:function(){}
}

IndexRecommendManager.prototype.init=function(opts){
	var that=this;
	if(!this.inited){
		this.options=$.extend(true,{},IndexRecommendManager.defValue,opts);
		this.appVM=new Vue(this.options.appVMConfig);
		that.initEventListener();
		that.loadRecommendList();
	}
	this.inited=true;
}

IndexRecommendManager.prototype.loadRecommendList=function(isAddModel){
	var that=this;
	this.shiwenIdList=[];
	var appVMConfigData=this.options.appVMConfig.data;
	var url=getShiciServiceHost()+'/shici/gushiwen/recommendList';
	muiAjaxGetJsonData({
		url:url,
		data:{},
		callback:function(data){
			if(data.entry){
				if(data.entry.length>0){
					if(!isAddModel){
						appVMConfigData.recommendList=[];
					}
					mui.each(data.entry,function(index,itemInfo){
					   var gushiwen=itemInfo["gushiwenVo"];
					   that.shiwenIdList.push(gushiwen.id);
					   gushiwen.shortContent=gushiwen.shortContent.trim();
					   replaceNextLine(gushiwen.shortContent,'content',true);
					   //itemInfo.gushiwenVo.mainImg='images/guofeng-banner00'+i+'.gif';
					   if(itemInfo.gushiwenVo.mainImg){
					   		itemInfo.gushiwenVo.bgStyle={
								height:'35vw',
								backgroundImage: 'url('+itemInfo.gushiwenVo.mainImg+')'
							}				   
					   }
					   if(!itemInfo.gushiwenWriterVo){
						   itemInfo.gushiwenWriterVo=that.options.defGushiwenWriterVo; 
					   }
					   //截取部分作者信息显示
					   if(itemInfo.gushiwenWriterVo.simpleIntro){
						   var tmpAttr=itemInfo.gushiwenWriterVo.simpleIntro.split(/？|。|！|，/);
						   var newIntro="";
						   if(tmpAttr.length>0){
							   newIntro=tmpAttr[0];
						   }
						   if(newIntro.length<15 && tmpAttr.length>1){
							   newIntro+=tmpAttr[1];
						   }
						   itemInfo.gushiwenWriterVo.simpleIntro=newIntro;
					   }
					   appVMConfigData.recommendList.push(itemInfo);
					});
				}
				that.loadRecommendListId();
				that.options.loadOver();
			}
		},
	});
}

IndexRecommendManager.prototype.loadRecommendListId=function(callBack){
	var that=this;
	this.shiwenIdWithMoreList=[];
	var url=getShiciServiceHost()+'/shici/gushiwen/recommendIdList';
	muiAjaxGetJsonData({
		url:url,
		data:{count:100},
		callback:function(data){
			console.log("loadRecommendListId")
			if(data.entry){
				that.shiwenIdWithMoreList = that.shiwenIdList.concat(data.entry);
				console.log(that.shiwenIdWithMoreList.length)
			}
		},
	});
}

IndexRecommendManager.prototype.initEventListener=function(){
	var that=this;
	//
	var writerIdAttrName="writer-id";
	var writerIdAttrEl="["+writerIdAttrName+"]";
	$(this.options.appVMConfig.el).on('tap',writerIdAttrEl,function(e){
		var writerId=$(this).closest(writerIdAttrEl).attr(writerIdAttrName);
		if(writerId && writerId!=0){
			console.log('writerId='+writerId);
			mui.openWindow({
				url:'pages/writerDetail.html?writerId='+writerId,
				id:'writerDetail.html-writerId-'+writerId,
				extras:{
					writerId : writerId
				}
			});
		}
	});
	//
	var shiwenIdAttrName="shiwen-id";
	var shiwenIdAttrEl="["+shiwenIdAttrName+"]";
	$(this.options.appVMConfig.el).on('tap',shiwenIdAttrEl,function(e){
		var shiwenId=$(this).closest(shiwenIdAttrEl).attr(shiwenIdAttrName);
		var toWithIdList=that.shiwenIdList;
		if(that.shiwenIdWithMoreList && that.shiwenIdWithMoreList.length>0){
			toWithIdList=that.shiwenIdWithMoreList;
		}
		console.log('shiwenId='+shiwenId);
		var timestamp=new Date().getTime();
		mui.openWindow({
			url:'pages/gushiwenDetail.html?infoId='+shiwenId+'&'+timestamp,
			id:'gushiwenDetail.html-infoId-'+shiwenId+'-'+timestamp,
			extras:{
				infoId : shiwenId,
				shiwenIdList : toWithIdList
			}
		});
	});
	//
	var tagIdAttrName="tag-id";
	var tagIdAttrEl="["+tagIdAttrName+"]";
	var tagNameAttrName="tag-name";
	$(this.options.appVMConfig.el).on('tap',tagIdAttrEl,function(e){
		var tagId=$(this).closest(tagIdAttrEl).attr(tagIdAttrName);
		var tagName=$(this).closest(tagIdAttrEl).attr(tagNameAttrName);
		console.log('tagId='+tagId);
		mui.openWindow({
			url:'pages/tagGushiwenList.html?tagId='+tagId,
			id:'tagGushiwenList.html-tagId-'+tagId,
			extras:{
				tagId : tagId,
				tagName : tagName
			}
		});
	});
	
}